package dhee.wtqshopproject.test;


import dhee.wtqshopproject.entity.User;
import dhee.wtqshopproject.utils.UserMapperUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@SpringBootTest
@RunWith(SpringRunner.class)
@ActiveProfiles("test")//声明一下测试活动
@Transactional
public class usertest {

    @Autowired
    private UserMapperUtils userMapperUtils;

    //测试注册
    @Test
    public void testzhuce(){
        User user = createTestUser();
        if(userMapperUtils.findByUsername(user.getUsername()).size()==0){
            System.out.println("可以注册");
            userMapperUtils.zhuceusername(user);
        }
    }
    //登录
    @Test
    public void testlogin(){
        User user = createTestUser();
   userMapperUtils.login(user);
    }

    // 测试全量查询
    @Test
    public void testFindAll(){
        List<User> users = UserMapperUtils.findall();
    }

    // 测试新增用户
    @Test
    public void testAddAndDelete(){
        // 准备测试数据
        User newUser = createTestUser();

        // 执行插入
        UserMapperUtils.insertUser(newUser);
        // 验证插入（通过全量查询）
        List<User> usersAfterInsert = UserMapperUtils.findall();
        System.out.println("usersAfterInsert = " + usersAfterInsert);

    }

    // 测试按ID查询
    @Test
    public void testFindById(){
        User user = UserMapperUtils.findById(1);
        System.out.println("user = " + user);
    }

@Test
public void testfindByUsername(){
        List<User> user = UserMapperUtils.findByUsername("username");
    System.out.println("user = " + user);
}

    // 测试更新用户
    @Test
    public void testUpdateUser(){
        // 准备数据
        User user = createTestUser();
        UserMapperUtils.insertUser(user);
   userMapperUtils.findall();
        // 修改数据
        user.setUsername("更新后的名字");
        user.setEmail("updated@example.com");
        UserMapperUtils.updateUser(user);
        userMapperUtils.findall();
    }

    // 测试删除用户
    @Test
    public void testDeleteUser(){
       userMapperUtils.deleteUser(1);
    }


    @Test
    public void testLikeselect(){
        User user = createTestUser();
        System.out.println("插入前的搜索情况"+UserMapperUtils.selectLike(user.getUsername()));
        userMapperUtils.insertUser(user);
        System.out.println("插入后的"+UserMapperUtils.selectLike(user.getUsername()));

    }

    // 创建测试用User对象
    private User createTestUser() {
        User user = new User();
        user.setUsername("查询测试10");
        user.setPassword("testpass");
        user.setEmail("test@test.com");
        user.setSex("1");
        return user;
    }
}
